package com.example.baseapp.admin.controller;

import com.example.baseapp.admin.dto.req.MenuReq;
import com.example.baseapp.admin.dto.resp.RoleMenuDto;
import com.example.baseapp.admin.entity.Menu;
import com.example.baseapp.admin.entity.RoleMenu;
import com.example.baseapp.admin.service.RoleMenuService;
import com.example.baseapp.admin.service.MenuService;
import com.example.baseapp.admin.service.impl.RoleMenuHandle;
import com.example.baseapp.admin.validator.interfaces.MenuParentIdExists;
import com.example.baseapp.admin.validator.interfaces.UniqueMenu;
import com.example.baseapp.common.exception.BusinessException;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 菜单表(Menu)表控制层
 *
 * @author zhanglei
 * @since 2022-04-28 14:24:39
 */
@RestController
@Validated
@RequestMapping("admin")
public class MenuController {
    /**
     * 服务对象
     */
    @Resource
    private MenuService menuService;
    @Resource
    private RoleMenuHandle roleMenuHandle;
    @Resource
    private RoleMenuService roleMenuService;

    /**
     * 通过主键查询单条数据
     *
     * @param roleId 主键
     * @return 单条数据
     */
    @GetMapping("/menus/{roleId}")
    public List<RoleMenuDto> getMenuByRoleId(@PathVariable("roleId") String roleId) {
        return roleMenuHandle.getRoleMenuRespByRoleId(roleId);
    }

    @PostMapping("/menus")
    public void createMenu(@UniqueMenu @Valid @RequestBody MenuReq menuReq) {
        Menu menu = this.setMenu(menuReq);
        //默认启用
        menu.setStatus("S");
        menuService.insert(menu);
        if (!CollectionUtils.isEmpty(menuReq.getRoles())) {
            //增加权限菜单关联信息
            roleMenuService.insert(
                    menuReq.getRoles().stream()
                            .map(roleId -> new RoleMenu().setRoleId(roleId).setMenuId(menuReq.getMenuId()))
                            .collect(Collectors.toList())
            );
        }

    }
    @PutMapping("/menus/{menuId}")
    public void updateMenu(@MenuParentIdExists @Valid @RequestBody MenuReq menuReq,@PathVariable("menuId") String menuId) {
        Menu menu = this.setMenu(menuReq);
        if (!StringUtils.hasText(menuId) || !menu.getMenuId().equals(menuId)){
            throw BusinessException.createErrorException("请求路径有误");
        }
        //更新菜单信息
        menuService.update(menu);
        roleMenuHandle.updateRoleMenu(menuReq.getRoles(), menuId);
    }

    /**
     *将menuReq转换为Menu
     * @param menuReq  menuReq
     * @return Menu
     */
    private Menu setMenu(MenuReq menuReq) {
        Menu menu = new Menu();
        BeanUtils.copyProperties(menuReq, menu);
        menu.setIsAffix(menuReq.getIsAffix());
        menu.setIsHide(menuReq.getIsHide());
        menu.setIsIframe(menuReq.getIsIframe());
        menu.setIsKeepAlive(menuReq.getIsKeepAlive());
        menu.setIsLink(menuReq.getIsLink());
        return menu;
    }

    /**
     * boolean转换为int
     * @param value Boolean
     * @return true:1 false:0
     */
    public int booleanToInteger(Boolean value) {
        if (value == null){
            value = false;
        }
        return value ? 1 : 0;
    }

}